package com.hp.attendance.serlvet.oneself;


import com.hp.attendance.entity.Leave;
import com.hp.attendance.service.OneselfService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//员工请假申请
@WebServlet("/oneselfLeaveUpdate")
public class OneselfLeaveUpdateServlet extends HttpServlet {
    private OneselfService oneselfService = new OneselfService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {

            int employeeNumber = Integer.parseInt(req.getParameter("employeeNumber"));
            int departmentNumber = Integer.parseInt(req.getParameter("departmentNumber"));
            Date start = new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("start"));
            Date end = new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("end"));
            List<Leave> list = oneselfService.getLeaveTime(employeeNumber, start, end);
            String days = req.getParameter("days");
            String type = req.getParameter("type");
            String reason = req.getParameter("reason");
            if (list.size()!=0){
                req.setAttribute("msg","请假日期错误请重新输入！");
                req.getRequestDispatcher("/leaveOneself").forward(req,resp);
            }else {
              Leave leave = new Leave(null,employeeNumber, departmentNumber, start, end, days, type, reason,null,null,null);
            leave.setStatus("未批准");

            oneselfService.insertOneselfLeave(leave);
            req.getRequestDispatcher("/oneselfPageLeave").forward(req,resp);}
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
